# $< 第一个输入 $^ 所有输入 $@ 输出
C_SOURCES = $(wildcard kernel/*.c cpu/*.c driver/*.c libc/*.c)
HEADERS = $(wildcard kernel/*.h cpu/*.h driver/*.h libc/*.h)
OBJS = ${C_SOURCES:.c=.o cpu/interrupt.o boot/boot.o boot/gdt.o}

CFLAGS = -g -ffreestanding -Wall -Wextra -fno-exceptions -m32

%.o: %.c ${HEADERS}
	i686-elf-gcc ${CFLAGS} -c $< -o $@

%.o: %.S
	i686-elf-as -o $@ $<

kernel.elf: ${OBJS}
	i686-elf-ld -o $@ -T linker.ld  $^

qemu: kernel.elf
	qemu-system-i386 -kernel kernel.elf -m 1G -device usb-ehci,id=ehci -drive if=none,id=stick,file=vudisk.img,format=raw -device usb-storage,bus=ehci.0,port=1,drive=stick

debug: kernel.elf
	qemu-system-i386 -kernel kernel.elf -m 1G -s -S -device usb-ehci,id=ehci -drive if=none,id=stick,file=vudisk.img,format=raw -device usb-storage,bus=ehci.0,port=1,drive=stick

gdb: kernel.elf
	gdb $^ -ex "target remote :1234" -ex "hb kernel_main" -ex "hb do_usb_init" -ex "c"

iso: kernel.elf
	cp kernel.elf isodir/boot/
	grub-mkrescue -o myosdev.iso isodir
	qemu-system-i386 -cdrom myosdev.iso

clean:
	rm -f *.o *.bin *.tmp
	rm -f boot/*.bin boot/*.o kernel/*.o libc/*.o cpu/*.o driver/*.o